﻿using System;
using GBlason.Culture;
using ResourceManager = System.Resources.ResourceManager;

namespace GBlason.ViewModel.CoatOfArm
{
    /// <summary>
    /// Presentation view model to store the data for the shape property.
    /// The shape is used as a coat of arm property. Or as a resource (to edit the coat of arm property)
    /// </summary>
    public class ShapeViewModel : EditableResourceViewModel, IEquatable<ShapeViewModel>
    {
        
        /// <summary>
        /// Gets the localized name of the shape. Only available for the default shapes defined in the application (no custom shapes).
        /// Return the technical name if no key is found in the resource files
        /// </summary>
        /// <value>
        /// The name of the localized.
        /// </value>
        public String LocalizedName
        {
            get
            {
                var manager = new ResourceManager(typeof(BlasonVocabulary));
                var name = manager.GetString(Name);
                return name ?? Name;
            }
        }

        private String _geometry;
        /// <summary>
        /// Gets or sets the geometry of the shape, define the whole coat of arm base shape.
        /// And is the limit of the display area for the CoA. Setting the geometry update the actual width and height
        /// </summary>
        /// <value>
        /// The geometry.
        /// </value>
        public String Geometry
        {
            get { return _geometry; }
            set
            {
                if (value == _geometry) return;
                _geometry = value;
                if (!String.IsNullOrEmpty(_geometry))
                {
                    var geo = System.Windows.Media.Geometry.Parse(Geometry);
                    ActualWidth = geo.Bounds.TopRight.X;
                    ActualHeight = geo.Bounds.BottomLeft.Y;
                }
                OnPropertyChanged("Geometry");
            }
        }

        
        /// <summary>
        /// Gets or sets the identifier. To be sure even custom added element get a unique identifier access
        /// </summary>
        /// <value>
        /// The identifier.
        /// </value>
        public Guid Identifier { get; set; }

        /// <summary>
        /// Gets or sets the width the user want to show the form (can be different from the original width)
        /// </summary>
        /// <value>
        /// The width of the prefered.
        /// </value>
        public ushort PreferedWidth { get; set; }

        /// <summary>
        /// Gets or sets the height of the prefered.
        /// </summary>
        /// <value>
        /// The height of the prefered.
        /// </value>
        public ushort PreferedHeight { get; set; }

        private Double _actualWidth;

        /// <summary>
        /// Gets the actual width.
        /// </summary>
        public Double ActualWidth
        {
            get { return _actualWidth; }
            private set
            {
                if (value == _actualWidth) return;
                _actualWidth = value;
                OnPropertyChanged("ActualWidth");
            }
        }

        private Double _actualHeight;

        /// <summary>
        /// Gets the actual height.
        /// </summary>
        public Double ActualHeight
        {
            get { return _actualHeight; }
            private set
            {
                if (value == _actualHeight) return;
                _actualHeight = value;
                OnPropertyChanged("ActualHeight");
            }
        }

        public bool Equals(ShapeViewModel p)
        {
            // If parameter is null, return false.
            if (ReferenceEquals(p, null))
            {
                return false;
            }

            // Optimization for a common success case.
            if (ReferenceEquals(this, p))
            {
                return true;
            }

            // If run-time types are not exactly the same, return false.
            if (GetType() != p.GetType())
                return false;

            return (Identifier == p.Identifier);
        }
    }
}
